package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.GwtUtil;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.common.PageNaviBar;
import com.ksyzt.gwt.client.common.SimpleMessage;
import com.ksyzt.gwt.client.common.SysResource;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.column.ColumnSelector;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.PageNewsData;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class NewsList extends MessageComposite {

	private static NewsListUiBinder uiBinder = GWT
			.create(NewsListUiBinder.class);

	interface NewsListUiBinder extends UiBinder<Widget, NewsList> {
	}

	@UiConstructor
	public NewsList() {
		initWidget(uiBinder.createAndBindUi(this));
		m_list.setWidth("100%");
	}

	COLUMNObj m_column;
	PageNewsData m_data;
	private AsyncCallback<PageNewsData> m_on_data = new AsyncCallback<PageNewsData>() {

		public void onSuccess(PageNewsData result) {
			m_data = result;
			list(null);
			message(SysConst.MSG_QUERY_DATA_SUCCESS);
		}

		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};
	/**
	 * 分页响应事件
	 */
	private MessageHandler m_navi_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.PAGE) {
				message(SysConst.MSG_QUERY_DATA);
				PageNaviBar.NaviData data = (PageNaviBar.NaviData) value;
				ServerProxy.SYS_MANAGER.getNews(m_column.getID(), data.page,
						data.size, m_on_data);
			}
		}
	};

	public void initList(COLUMNObj column) {
		m_column = column;
		lbOrgName.setText(column.getNAME());
		ServerProxy.SYS_MANAGER.getNews(m_column.getID(), 0, 20, m_on_data);
		navi.addMessageHandler(m_navi_handler);

	}

	@UiField
	Label lbOrgName;
	@UiField
	PageNaviBar navi;

	@UiField
	Grid m_list;

	private ClickHandler m_item_click = new ClickHandler() {

		public void onClick(ClickEvent event) {
			Anchor a = (Anchor) event.getSource();
			VW_NEWSObj n = (VW_NEWSObj) a.getElement().getPropertyObject("v");
			MessageEvent ev = new MessageEvent(MessageEvent.ITEMCLICK, n);
			fireEvent(ev);
		}
	};
	private AsyncCallback<Void> m_on_change_data = new AsyncCallback<Void>() {

		public void onSuccess(Void result) {
			initList(m_column);
		}

		public void onFailure(Throwable caught) {
			message(caught.getMessage());
		}
	};
	private MessageHandler m_column_select_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.OK) {
				COLUMNObj col = (COLUMNObj) value;
				if (selectionids.length() > 0) {
					ServerProxy.SYS_MANAGER.changeNewsOid(selectionids,
							col.getID(), m_on_change_data);
				}
			}
			ColumnSelector dlg = (ColumnSelector) sender;
			dlg.hide(true);

		}
	};

	private AsyncCallback<Void> m_delete_handler = new AsyncCallback<Void>() {

		@Override
		public void onSuccess(Void result) {

			list(null);
		}

		@Override
		public void onFailure(Throwable caught) {
			message(caught.getMessage());
		}
	};

	private ClickHandler m_item_delete_click = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			Anchor a = (Anchor) event.getSource();
			VW_NEWSObj n = (VW_NEWSObj) a.getElement().getPropertyObject("v");

			boolean b = Window.confirm("删除：" + n.getTITLE() + "?");
			if (b == true) {
				m_data.news.remove(n);
				ServerProxy.SYS_MANAGER.deleteNews(n.getID(), m_delete_handler);
			}

		}
	};

	@UiHandler("btnList")
	void list(ClickEvent e) {
		m_list.clear();

		if (m_data != null) {
			GWT.log(m_data.currentPage+"/"+m_data.pagesize+"/"+m_data.total);
			navi.setData(m_data.currentPage, (int) m_data.total,
					m_data.pagesize);

			m_list.resize(m_data.news.size(), 5);
			m_list.setCellPadding(3);
			m_list.setCellSpacing(0);
			int row = 0;
			int col = 0;
			ColumnFormatter cf = m_list.getColumnFormatter();

			RowFormatter rf = m_list.getRowFormatter();
			CellFormatter cellf = m_list.getCellFormatter();

			cf.setWidth(0, "80");
			cf.setWidth(1, "25");
			// cf.setStyleName(2, SysResource.INSTANCE.getCss().row_buttons());

			for (VW_NEWSObj n : m_data.news) {
				col = 0;
				rf.setStyleName(row, "site-Row" + (row % 2));
				m_list.setWidget(row, col++, new CheckBox(n.getID() + ""));

				if (n.getICON() != null && n.getICON().length() > 0) {
					Image img = new Image(SysResource.INSTANCE.col_picture());
					img.setHeight("16px");
					img.setWidth("16px");
					m_list.setWidget(row, col++, img);
				} else {
					m_list.setWidget(row, col++, new Label(""));
				}

				String html = "<div class='site-Title'>" + n.getTITLE()
						+ "</div>";
				html += "<div class='site-description'>";
				if (n.getUPTOP() > 0) {
					html += "<img src='"
							+ SysResource.INSTANCE.uptop().getSafeUri().asString()
							+ "' />";
				}
				if (n.getLASTED() > 0) {
					html += "<img src='"
							+ SysResource.INSTANCE.newicon().getSafeUri().asString()
							+ "' />";
				}
				html += "<span class='site-date'>"
						+ GwtUtil.formatTimestamp(n.getADDTIME(), true)
						+ "</span>";
				html += "<span class='site-summary'>" + n.getSUMMARY()
						+ "</span>";
				html += "</div>";

				HTMLPanel fp = new HTMLPanel(html);
				m_list.setWidget(row, col++, fp);

				final String WIDTH = "35px";
				Anchor a = new Anchor("");
				a.setStyleName("btn_edit");
				a.addClickHandler(m_item_click);
				a.getElement().setPropertyObject("v", n);
				m_list.setWidget(row, col++, a);
				cellf.setAlignment(row, col - 1,
						HasHorizontalAlignment.ALIGN_RIGHT,
						HasVerticalAlignment.ALIGN_MIDDLE);
				cellf.setWidth(row, col - 1, WIDTH);

				a = new Anchor("");
				a.setStyleName("btn_delete");
				a.addClickHandler(m_item_delete_click);
				a.getElement().setPropertyObject("v", n);
				m_list.setWidget(row, col++, a);
				cellf.setAlignment(row, col - 1,
						HasHorizontalAlignment.ALIGN_RIGHT,
						HasVerticalAlignment.ALIGN_MIDDLE);
				cellf.setWidth(row, col - 1, WIDTH);
				row++;
			}

			if (m_data.news.size() == 0) {
				SimpleMessage m = new SimpleMessage(SysConst.MSG_NO_RESULT);
				m_list.resize(2, 1);
				cellf.setHeight(0, 0, "100px");
				m_list.setWidget(1, 0, m);
				cf.setWidth(0, "100%");
			} else {
				cellf.setHeight(0, 0, "");
			}
		}
	}

	@UiHandler("btnCreate")
	void create(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.ITEMCLICK, null);
		fireEvent(ev);
	}

	@UiHandler("btnRefresh")
	void refresh(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.REFRESH, null);
		fireEvent(ev);
	}

	String selectionids;

	@UiHandler("btnTrans")
	void transfor(ClickEvent e) {

		String sb = "";
		for (int i = 0; i < m_list.getRowCount(); i++) {
			CheckBox cb = (CheckBox) m_list.getWidget(i, 0);
			if (cb != null && cb.getValue() == true) {
				if (sb.length() > 0) {
					sb += "," + cb.getText();
				} else {
					sb += cb.getText();
				}
			}
		}
		selectionids = sb;

		if (selectionids.equals("")) {
			message(SysConst.MSG_NEED_SELECT_DATA);
		} else {
			ColumnSelector dlg = new ColumnSelector();
			dlg.addMessageHandler(m_column_select_handler);

			dlg.init(m_userid, OrgType.OT_NEWS);
			dlg.setModal(true);
			dlg.show();
			dlg.center();

		}

	}

	int m_userid;

	public void setUserId(int userid) {
		m_userid = userid;

	}
}
